package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.system.SystemConfig;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.db.util.UserOrderCallback;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.UserInfoService;
import org.linlinjava.litemall.wx.service.WxSpellService;
import org.linlinjava.litemall.wx.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Autowired
    private LitemallOrderService orderService;

    @Autowired
    private LitemallUserService userService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private LitemallCouponUserService couponUserService;

    @Autowired
    private LitemallCollectService collectService;

    @Autowired
    private WxSpellService spellService;

    @Autowired
    private LitemallSystemConfigService systemConfigService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     * 加会员卡信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("index")
    public Object list(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
//        LitemallMemberCard mCard = memberCardService.query(); // 会员卡
        Long couponCount = couponUserService.queryUsableCount(userId);
//        LitemallMember member = memberService.queryByUid(userId); // 会员信息
        int collectCount = collectService.count(userId);// 收藏数
        LitemallUser user = userService.findById(userId);
        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("order", orderService.orderInfo(userId, new UserOrderCallback() {
            private Integer unSpellNum = 3; // 只处理三条数据

            @Override
            public Map<String, Object> getUnSpellExt(LitemallOrder order) {
                if (this.unSpellNum <= 0) {
                    return null;
                } else {
                    Map<String, Object> re = spellService.spellByOrder(userId, order.getId());
                    this.unSpellNum--;
                    return re;
                }
            }
        }));
//        data.put("mCard", mCard);
//        data.put("member", member);
        data.put("couponCount", couponCount);
//        data.put("couponCount", couponCount);
        data.put("balance", user.getBalance());
        data.put("lessOutAmount", SystemConfig.getLessOutAmount());
        data.put("collectCount", collectCount);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/team")
    public Object team(@LoginUser Integer userId) {
        List<UserNode> data = userService.queryTeam(userId);
        return ResponseUtil.ok(UserNode.buildTree(data));
    }

//    /**
//     * 个人中心配置信息
//     * @return 个人中心配置信息
//     */
//    @GetMapping("/setting")
//    public Object setting() {
//        Map<String, Object> cr = new HashMap<>();
//        LitemallMemberCard card = memberCardService.query();
//        cr.put("costMoneyUnit", card.getCostMoneyUnit());
//        cr.put("increaseBonus", card.getIncreaseBonus());
//        cr.put("leastMoneyToUseBonus", card.getLeastMoneyToUseBonus());
//        cr.put("costBonusUnit", card.getCostBonusUnit());
//        cr.put("reduceMoney", card.getReduceMoney());
//        cr.put("maxReduceBonus",card.getMaxReduceBonus());
//        cr.put("maxIncreaseBonus",card.getMaxIncreaseBonus());
//        Map<String, String> shareConfig = systemConfigService.listShare();
//        Map<String, Object> br = new HashMap<>();
//        br.put("orderBenefitType", shareConfig.get("litemall_share_order_benefit_type"));
//        br.put("orderBenefit", shareConfig.get("litemall_share_order_benefit"));
//        br.put("loginBenefit", shareConfig.get("litemall_share_login_benefit"));
//        Map<String, Object> re = new HashMap<>();
//        re.put("cardRules", cr);
//        re.put("benefitRules", br);
//        return ResponseUtil.ok(re);
//    }

    /**
     * 个人中心配置信息
     *
     * @return 个人中心配置信息
     */
    @PostMapping("/applyForReseller")
    public Object applyForReseller(@RequestBody LitemallResellerApply apply) {
        return wxUserService.applyForReseller(apply);
    }

    /**
     * 判断当前用户是否是会员
     *
     * @param userId
     * @return
     */
    @GetMapping("/isMember")
    public Boolean isMember(@LoginUser Integer userId) {
        return wxUserService.isMember(userId);
    }

    /**
     * 判断是否交押金
     *
     * @param userId
     * @return
     */
    @GetMapping("/deposit")
    public Boolean hasPayDeposit(@LoginUser Integer userId) {
        return wxUserService.hasPayDeposit(userId);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/userInfo")
    public Object getUserInfo(@LoginUser Integer userId) {
        return ResponseUtil.ok(userInfoService.getUserInfo(userId));
    }
}